#!/usr/bin/env /usr/local/python3
# -*- coding: utf-8 -*-

"""
python 操作RabbitMQ
"""

__author__ = 'hubert'

import pika
import time

# 河北MQ信息
host = '175.6.15.136'
port = 5672

# 河北 hebeiiptv_new
# 甘肃gansudianxin_iptv
# 陕西电信 shanxi_dianxin_pake
userName = 'hebeiiptv_new'

# 河北 q\Yf|en>4rif+]
# 甘肃b;(qV+q!jz]L#%
# 陕西电信
passWord = 'q\Yf|en>4rif+]'
# test_vhosts cms_host
virtualHost = 'cms_host'


# basicget 轮询
def customer_get():
    # 设置用户密码
    credentials = pika.PlainCredentials(userName, passWord)
    # 设置IP、端口、vhost信息
    connection = pika.BlockingConnection(pika.ConnectionParameters(host, port, virtualHost, credentials))
    # 创建频道
    channel = connection.channel()

    # 返回多个值(3个值)，为list,测试队列为Python,
    # 具体返回情况，可参阅basic_get源码，河北hebei_liantong_iptv_assetInf
    # gansudianxin_iptv_assetInfo
    # 陕西电信 shanxi_dianxin_pake_assetInfo
    response = channel.basic_get('hebei_liantong_iptv_assetInf', no_ack=False)
    delivery_tag = 0
    # 判断是否为空
    if response[2] is None:
        print('队列无内容!', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
    else:
        # b'Python1'表示bytes类型,response[0]为类对象，对于类对象，可直接通过类获取类属性(面向对象编程)
        print('MQ消息为:', response[2].decode('utf-8'))
        delivery_tag = response[0].delivery_tag
        print('当前时间：', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
        print('剩余消息:', response[0].message_count)
        print('返回结果:', response[0].delivery_tag)

    # 若不执行反馈，MQ数据不会删除，永远只能取队列第一条数据

    # channel.basic_ack(delivery_tag=delivery_tag)
    connection.close()


if __name__ == '__main__':
    customer_get()




